'use strict';

const Controller = require('egg').Controller;

class RoleController extends Controller {
    async create() {
        let { ctx, app } = this;
        ctx.validate({
            rolename: {
                type: "string",
                required: true,
                desc: "权限名称"
            },
            sortnum: {
                type: "string",
                required: true,
                desc: '权限排序',
            },
            menuids: {
                type: "string",
                required: true,
                desc: '权限菜单',
            },
            userids: {
                type: "string",
                required: true,
                desc: '权限用户',
            }
        });
        let { rolename, sortnum, menuids, userids } = ctx.request.body
        if (await app.model.Role.findOne({
            where: { rolename }
        })) {
            ctx.throw(400, "权限名称已存在")
        }
        let res = await app.model.Role.create({ rolename, sortnum, menuids, userids })
        this.updateUserNicknameAsRoleId(res.userids, res.id)
        return ctx.apiSuccess(res)
    }

    async allList() {
        let { ctx, app } = this;
        let res = await ctx.page(
            app.model.Role,
            {},
            {
                attributes: ["id", "rolename","sortnum","menuids","userids"],
                order: [['sortnum', 'ASC']]
            }
        );
        return ctx.apiSuccess(res);
    }

    async getRoleOne() {
        let { ctx, app } = this;
        ctx.validate(
            {
                id: {
                    type: "string",
                    required: true,
                    desc: "权限ID",
                }
            }
        );
        const id = ctx.query.id
        let res = await app.model.Role.findOne({
            where: {
                id
            },
            attributes: [
                "id",
                "rolename",
                "sortnum",
                "menuids",
                "userids"
            ]
        })
        return ctx.apiSuccess(res);
    }

    async updateRoleOne() {
        let { ctx, app } = this;
        ctx.validate({
            roleId: {
                type: "string",
                required: true,
                desc: "权限Id"
            },
            rolename: {
                type: "string",
                required: true,
                desc: "权限名称"
            },
            sortnum: {
                type: "string",
                required: true,
                desc: '权限排序',
            },
            menuids: {
                type: "string",
                required: true,
                desc: '权限菜单',
            },
            userids: {
                type: "string",
                required: true,
                desc: '权限用户',
            }
        });
        const { roleId, rolename, sortnum, menuids, userids } = ctx.request.body
        let role = await app.model.Role.findOne({
            where: {
                id: roleId
            },
            attributes: [
                "id",
                "rolename",
                "sortnum",
                "menuids",
                "userids"
            ]
        })
        if (!role) {
            ctx.throw(400, "获取权限信息失败");
        }
        let res = await role.update({ id: roleId, rolename, sortnum, menuids, userids })
        this.updateUserNicknameAsRoleId(userids, roleId)
        return ctx.apiSuccess(res);
    }

    async removeRole() {
        let { ctx, app } = this;
        ctx.validate({
            roleId: {
                type: "string",
                required: true,
                desc: "权限Id"
            }
        });
        const { roleId } = ctx.request.body
        let role = await app.model.Role.findOne({
            where: {
                id: roleId
            },
            attributes: [
                "id",
                "rolename",
                "sortnum",
                "menuids",
                "userids"
            ]
        })
        if (!role) {
            ctx.throw(400, "获取菜单信息失败");
        }
        await role.destroy({
            where: {
                id: roleId,
            },
        });
        return ctx.apiSuccess("删除成功")
    }

    updateUserNicknameAsRoleId(userids, roleId){
        let { app } = this;
        let bb = userids.length > 0 ? userids.split(',') : []
        if(bb.length > 0){
            bb.forEach(async item => {
                 let user = await app.model.User.findOne({
                    where:{
                        id: item
                    }
                })
                let aa = user.nickname.length > 0 ? (user.nickname).split(',') : []
                if(aa.length > 0){
                    if(bb.length > 0){
                        if(!(aa.includes(roleId))){
                            aa.push(roleId)
                            await user.update({ id: item, nickname: aa.join(",") })
                        }
                    }
                }else{
                    await user.update({ id: item, nickname: roleId })
                } 
            });
        }       
    }
}

module.exports = RoleController;
